package com.dios.mirage.component;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;

public class PictureBackgroundButtonField extends Field {
	private String _label;
	private int _labelHeight;
	private int _labelWidth;
	private Font _font;

	private Bitmap _currentPicture;
	private Bitmap _onPicture;
	private Bitmap _offPicture;

	public PictureBackgroundButtonField(int width, int height, long style,
			Bitmap picture, Bitmap selectedPic) {
		super(style);

		_font = getFont();
		_label = "";
		_labelHeight = height;
		_labelWidth = width;
		_currentPicture = picture;
		_onPicture = selectedPic;
		_offPicture = picture;
	}

	public int getPreferredHeight() {
		return _labelHeight;
	}

	public int getPreferredWidth() {
		return _labelWidth;
	}

	protected void onFocus(int direction) {
		_currentPicture = _onPicture;
		invalidate();
	}

	protected void onUnfocus() {
		_currentPicture = _offPicture;
		invalidate();
	}

	protected void drawFocus(Graphics graphics, boolean on) {
		// Do nothing
	}

	protected void layout(int width, int height) {
		setExtent(getPreferredWidth(), getPreferredHeight());
	}

	protected void paint(Graphics graphics) {
		graphics.drawBitmap(0, 0, getPreferredWidth(), getPreferredHeight(),
				_currentPicture, 0, 0);
		graphics.setFont(_font);
		graphics.drawText(
				_label,
				4,
				2,
				(int) (getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK),
				getWidth() - 6);
	}

	protected boolean navigationClick(int status, int time) {
		fieldChangeNotify(1);
		return true;
	}
}
